home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / jpegsrc4.zip / JDSAMPLE.C < prev    next >
C/C++ Source or Header  |  1992-11-05  |  10KB  |  289 lines

  1. /*
  2.  * jdsample.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains upsampling routines.
  9.  * These routines are invoked via the upsample and
  10.  * upsample_init/term methods.
  11.  *
  12.  * An excellent reference for image resampling is
  13.  *   Digital Image Warping, George Wolberg, 1990.
  14.  *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
  15.  */
  16.  
  17. #include "jinclude.h"
  18.  
  19.  
  20. /*
  21.  * Initialize for upsampling a scan.
  22.  */
  23.  
  24. METHODDEF void
  25. upsample_init (decompress_info_ptr cinfo)
  26. {
  27.   /* no work for now */
  28. }
  29.  
  30.  
  31. /*
  32.  * Upsample pixel values of a single component.
  33.  * This version handles any integral sampling ratios.
  34.  *
  35.  * This is not used for typical JPEG files, so it need not be fast.
  36.  * Nor, for that matter, is it particularly accurate: the algorithm is
  37.  * simple replication of the input pixel onto the corresponding output
  38.  * pixels.  The hi-falutin sampling literature refers to this as a
  39.  * "box filter".  A box filter tends to introduce visible artifacts,
  40.  * so if you are actually going to use 3:1 or 4:1 sampling ratios
  41.  * you would be well advised to improve this code.
  42.  */
  43.  
  44. METHODDEF void
  45. int_upsample (decompress_info_ptr cinfo, int which_component,
  46.           long input_cols, int input_rows,
  47.           long output_cols, int output_rows,
  48.           JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  49.           JSAMPARRAY output_data)
  50. {
  51.   jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
  52.   register JSAMPROW inptr, outptr;
  53.   register JSAMPLE invalue;
  54.   register short h_expand, h;
  55.   short v_expand, v;
  56.   int inrow, outrow;
  57.   register long incol;
  58.  
  59. #ifdef DEBUG            /* for debugging pipeline controller */
  60.   if (input_rows != compptr->v_samp_factor ||
  61.       output_rows != cinfo->max_v_samp_factor ||
  62.       (input_cols % compptr->h_samp_factor) != 0 ||
  63.       (output_cols % cinfo->max_h_samp_factor) != 0 ||
  64.       output_cols*compptr->h_samp_factor != input_cols*cinfo->max_h_samp_factor)
  65.     ERREXIT(cinfo->emethods, "Bogus upsample parameters");
  66. #endif
  67.  
  68.   h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
  69.   v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
  70.  
  71.   outrow = 0;
  72.   for (inrow = 0; inrow < input_rows; inrow++) {
  73.     for (v = 0; v < v_expand; v++) {
  74.       inptr = input_data[inrow];
  75.       outptr = output_data[outrow++];
  76.       for (incol = 0; incol < input_cols; incol++) {
  77.     invalue = GETJSAMPLE(*inptr++);
  78.     for (h = 0; h < h_expand; h++) {
  79.       *outptr++ = invalue;
  80.     }
  81.       }
  82.     }
  83.   }
  84. }
  85.  
  86.  
  87. /*
  88.  * Upsample pixel values of a single component.
  89.  * This version handles the common case of 2:1 horizontal and 1:1 vertical.
  90.  *
  91.  * The upsampling algorithm is linear interpolation between pixel centers,
  92.  * also known as a "triangle filter".  This is a good compromise between
  93.  * speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
  94.  * of the way between input pixel centers.
  95.  */
  96.  
  97. METHODDEF void
  98. h2v1_upsample (decompress_info_ptr cinfo, int which_component,
  99.            long input_cols, int input_rows,
  100.            long output_cols, int output_rows,
  101.            JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  102.            JSAMPARRAY output_data)
  103. {
  104.   register JSAMPROW inptr, outptr;
  105.   register int invalue;
  106.   int inrow;
  107.   register long colctr;
  108.  
  109. #ifdef DEBUG            /* for debugging pipeline controller */
  110.   jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
  111.   if (input_rows != compptr->v_samp_factor ||
  112.       output_rows != cinfo->max_v_samp_factor ||
  113.       (input_cols % compptr->h_samp_factor) != 0 ||
  114.       (output_cols % cinfo->max_h_samp_factor) != 0 ||
  115.       output_cols*compptr->h_samp_factor != input_cols*cinfo->max_h_samp_factor)
  116.     ERREXIT(cinfo->emethods, "Bogus upsample parameters");
  117. #endif
  118.  
  119.   for (inrow = 0; inrow < input_rows; inrow++) {
  120.     inptr = input_data[inrow];
  121.     outptr = output_data[inrow];
  122.     /* Special case for first column */
  123.     invalue = GETJSAMPLE(*inptr++);
  124.     *outptr++ = (JSAMPLE) invalue;
  125.     *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(*inptr) + 2) >> 2);
  126.  
  127.     for (colctr = input_cols - 2; colctr > 0; colctr--) {
  128.       /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
  129.       invalue = GETJSAMPLE(*inptr++) * 3;
  130.       *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(inptr[-2]) + 2) >> 2);
  131.       *outptr++ = (JSAMPLE) ((invalue + GETJSAMPLE(*inptr) + 2) >> 2);
  132.     }
  133.  
  134.     /* Special case for last column */
  135.     invalue = GETJSAMPLE(*inptr);
  136.     *outptr++ = (JSAMPLE) ((invalue * 3 + GETJSAMPLE(inptr[-1]) + 2) >> 2);
  137.     *outptr++ = (JSAMPLE) invalue;
  138.   }
  139. }
  140.  
  141.  
  142. /*
  143.  * Upsample pixel values of a single component.
  144.  * This version handles the common case of 2:1 horizontal and 2:1 vertical.
  145.  *
  146.  * The upsampling algorithm is linear interpolation between pixel centers,
  147.  * also known as a "triangle filter".  This is a good compromise between
  148.  * speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
  149.  * of the way between input pixel centers.
  150.  */
  151.  
  152. METHODDEF void
  153. h2v2_upsample (decompress_info_ptr cinfo, int which_component,
  154.            long input_cols, int input_rows,
  155.            long output_cols, int output_rows,
  156.            JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  157.            JSAMPARRAY output_data)
  158. {
  159.   register JSAMPROW inptr0, inptr1, outptr;
  160. #ifdef EIGHT_BIT_SAMPLES
  161.   register int thiscolsum, lastcolsum, nextcolsum;
  162. #else
  163.   register INT32 thiscolsum, lastcolsum, nextcolsum;
  164. #endif
  165.   int inrow, outrow, v;
  166.   register long colctr;
  167.  
  168. #ifdef DEBUG            /* for debugging pipeline controller */
  169.   jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
  170.   if (input_rows != compptr->v_samp_factor ||
  171.       output_rows != cinfo->max_v_samp_factor ||
  172.       (input_cols % compptr->h_samp_factor) != 0 ||
  173.       (output_cols % cinfo->max_h_samp_factor) != 0 ||
  174.       output_cols*compptr->h_samp_factor != input_cols*cinfo->max_h_samp_factor)
  175.     ERREXIT(cinfo->emethods, "Bogus upsample parameters");
  176. #endif
  177.  
  178.   outrow = 0;
  179.   for (inrow = 0; inrow < input_rows; inrow++) {
  180.     for (v = 0; v < 2; v++) {
  181.       /* inptr0 points to nearest input row, inptr1 points to next nearest */
  182.       inptr0 = input_data[inrow];
  183.       if (v == 0) {        /* next nearest is row above */
  184.     if (inrow == 0)
  185.       inptr1 = above[input_rows-1];
  186.     else
  187.       inptr1 = input_data[inrow-1];
  188.       } else {            /* next nearest is row below */
  189.     if (inrow == input_rows-1)
  190.       inptr1 = below[0];
  191.     else
  192.       inptr1 = input_data[inrow+1];
  193.       }
  194.       outptr = output_data[outrow++];
  195.  
  196.       /* Special case for first column */
  197.       thiscolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  198.       nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  199.       *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
  200.       *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 8) >> 4);
  201.       lastcolsum = thiscolsum; thiscolsum = nextcolsum;
  202.  
  203.       for (colctr = input_cols - 2; colctr > 0; colctr--) {
  204.     /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
  205.     /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
  206.     nextcolsum = GETJSAMPLE(*inptr0++) * 3 + GETJSAMPLE(*inptr1++);
  207.     *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
  208.     *outptr++ = (JSAMPLE) ((thiscolsum * 3 + nextcolsum + 8) >> 4);
  209.     lastcolsum = thiscolsum; thiscolsum = nextcolsum;
  210.       }
  211.  
  212.       /* Special case for last column */
  213.       *outptr++ = (JSAMPLE) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
  214.       *outptr++ = (JSAMPLE) ((thiscolsum * 4 + 8) >> 4);
  215.     }
  216.   }
  217. }
  218.  
  219.  
  220. /*
  221.  * Upsample pixel values of a single component.
  222.  * This version handles the special case of a full-size component.
  223.  */
  224.  
  225. METHODDEF void
  226. fullsize_upsample (decompress_info_ptr cinfo, int which_component,
  227.            long input_cols, int input_rows,
  228.            long output_cols, int output_rows,
  229.            JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  230.            JSAMPARRAY output_data)
  231. {
  232. #ifdef DEBUG